package com.csun.nusing.presenter;

import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.widget.EditText;

import com.csun.nusing.R;
import com.csun.nusing.bean.UrlBean;
import com.csun.nusing.bean.jsonbean.LoginJsonBean;
import com.csun.nusing.bean.jsonbean.UserInfoJsonBean;
import com.csun.nusing.bean.jsonbean.UserInfoJsonBean.UserInfoDataBean;
import com.csun.nusing.presenter.inter.ILogin;
import com.csun.nusing.ui.activity.NursingMainActivity;
import com.csun.nusing.ui.familyactivity.FamilyMainActivity;
import com.csun.nusing.util.IntentUtils;
import com.csun.nusing.util.SPUtils;
import com.csun.nusing.util.StringUtils;
import com.csun.nusing.util.ToastUtil;
import com.google.gson.Gson;
import com.home.administrator.okhttplibrary.IOkTaskCallback;
import com.home.administrator.okhttplibrary.OkHttpTask;
import com.home.administrator.okhttplibrary.OkHttpTask.OkHttpTaskBuidler.OkType;

import java.util.HashMap;

/**
 * 登录页面的presenter中间层 ClassName: LoginPresenter
 * 
 * @Description: 登录操作
 * @author fzq
 * @date 2017-10-20
 */
public class LoginPresenter extends BasePresenter<ILogin> {

	private String username;
	private String password;
	protected int userId;
	protected String token;
	protected UserInfoJsonBean userInfoJsonBean;
	private Boolean isNursingFlag;// 是否护工
	private boolean haveNursingRoleFalg;// 拥有护工权限
	private boolean haveFamilyRoleFalg;// 拥有家属角色

	public LoginPresenter(ILogin view) {
		super(view);
	}

	/**
	 * 处理登录
	 * 
	 * @Description: TODO
	 * @param @param context
	 * @param @param username
	 * @param @param password
	 * @param @return
	 * @return boolean 是否登录成功
	 * @throws
	 * @author fzq
	 * @date 2017-10-20
	 */
	public boolean login(Context context, EditText useredit, EditText psdedit) {
		username = useredit.getText().toString().trim();
		password = psdedit.getText().toString().trim();
		if (!StringUtils.isEmpty(username) && !StringUtils.isEmpty(password)) {
			Login(context);
		} else {
			ToastUtil.showMessage(context, R.string.username_password_not_empty);
		}
		return true;
	}

	/**
	 * 登录,跳转到主界面
	 * 
	 * @Description: TODO
	 * @param @param context
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-10-20
	 */
	private void Login(final Context context) {
		// RequestBody requestBodyPost = new FormBody.Builder()
		// .add("username", username)
		// .add("code", "news")
		// .add("pageSize", "20")
		// .add("parentid", "0")
		// .add("type", "1")
		// .build();
		HashMap<String, String> param = new HashMap<String, String>();
		param.put("username", username);
		param.put("password", password);
		OkHttpTask.newInstance(context).post(param, OkType.form)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "---------- " + result);
							Gson gson = new Gson();
							LoginJsonBean loginJsonBean = gson.fromJson(result,
									LoginJsonBean.class);
							if (loginJsonBean != null) {
								if (loginJsonBean.isStatus()) {
									userId = loginJsonBean.getData()
											.getUserId();
									token = loginJsonBean.getData().getToken();
									SPUtils.put(context, "USERNAME", ""
											+ username);
									SPUtils.put(context, "PASSWORD", ""
											+ password);
									SPUtils.put(context, "userId", "" + userId);
									SPUtils.put(context, "authorization",
											userId + "_" + token);
									getUserInfo(context, userId + "_" + token);
								} else {
									ToastUtil.showMessage(context,
											loginJsonBean.getMessage());
								}
							}
						}
					}
				}).start(UrlBean.LOGINURL);
	}

	/**
	 * 获取用户的角色
	 * 
	 * @Description: TODO
	 * @param @param context
	 * @param @param authorization
	 * @return void
	 * @throws
	 * @author fzq
	 * @date 2017-11-8
	 */
	protected void getUserInfo(final Context context, String authorization) {
		OkHttpTask.newInstance(context)
				.addHeader("authorization", authorization)
				.enqueue(new IOkTaskCallback() {
					@Override
					public void onSuccess(String result) {
						if (result != null) {
							Log.e("fzq", "---" + result);
							Gson gson = new Gson();
							userInfoJsonBean = gson.fromJson(result,
									UserInfoJsonBean.class);
							if (userInfoJsonBean != null
									&& userInfoJsonBean.getData() != null) {
								controlData(context, userInfoJsonBean.getData());
							}
						}
					}
				}).start(UrlBean.USERINFOURL);

	}

	/**
	 * 登录
	 * @Description: TODO
	 * @param @param context
	 * @param @param data
	 * @return void
	 * @author fzq
	 * @date 2017-11-8
	 */
	protected void controlData(Context context, UserInfoDataBean data) {
		// roleID 3 家属
		// role id 4 护工
		if (data.getRoles() != null) {
			isNursingFlag = (Boolean) SPUtils.get(context, "isNursingFlag",
					false);
			for (int i = 0; i < data.getRoles().size(); i++) {
				if (data.getRoles().get(i).getRoleid() == 3) {
					haveFamilyRoleFalg = true;
				} else if (data.getRoles().get(i).getRoleid() == 4) {
					haveNursingRoleFalg = true;
				}
			}
			// 假如有护工登录记录
			if (isNursingFlag) {
				if (haveNursingRoleFalg) {
					SPUtils.put(context, "isNursingFlag", true);
					IntentUtils.startActivity(context,
							NursingMainActivity.class);
					((Activity) context).finish();
				} else if (haveFamilyRoleFalg) {
					SPUtils.put(context, "isNursingFlag", false);
					IntentUtils
					.startActivity(context, FamilyMainActivity.class);
					((Activity) context).finish();
				} else {
					ToastUtil.showMessage(context, R.string.no_role_login);
				}
				// 假如没有
			} else {
				if (haveFamilyRoleFalg) {
					SPUtils.put(context, "isNursingFlag", false);
					IntentUtils
					.startActivity(context, FamilyMainActivity.class);
					((Activity) context).finish();
				} else if (haveNursingRoleFalg) {
					SPUtils.put(context, "isNursingFlag", true);
					IntentUtils.startActivity(context,
							NursingMainActivity.class);
					((Activity) context).finish();
				} else {
					ToastUtil.showMessage(context, R.string.no_role_login);
				}
			}

		}
	}

}
